//
//  ComplaintBusinessIphoneCell.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/22.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatComplaintIphoneCell.h"

@interface MeatComplaintIphoneCell()<UITextFieldDelegate>

@end

@implementation MeatComplaintIphoneCell

- (void)setupUI{
    
    [self addSubview:self.phoneLab];
    [self addSubview:self.phoneNumTextField];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    _phoneLab.mj_x = 15;
    _phoneLab.mj_y = (self.height - _phoneLab.height)/2.0;
    
    _phoneNumTextField.mj_y = (self.height - _phoneNumTextField.height)/2.0;
    _phoneNumTextField.mj_x = self.phoneLab.right + 20;
    _phoneNumTextField.width = self.width - _phoneNumTextField.left - 15;
}


#pragma mark - delegate

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSInteger strLength = textField.text.length - range.length + string.length;
    if (strLength > 11){
        return NO;
    }
    NSString *text = nil;
    //如果string为空，表示删除
    if (string.length > 0) {
        text = [NSString stringWithFormat:@"%@%@",textField.text,string];
    }else{
        text = [textField.text substringToIndex:range.location];
    }
    
    if (text.length > 11) {
        if (![text isMobileNumber]) {
            self.phoneNumTextField.text = @"";
            self.phoneNumTextField.placeholder = @"请输入您正确的手机号";
        }else{
            NSString *numberString = [text stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
            self.phoneNumTextField.text = numberString;
        }
        return NO;
    }
    
  return YES;
}


#pragma mark - setter and getter

- (UILabel *)phoneLab{
    
    if (!_phoneLab) {
        _phoneLab = [[UILabel alloc] init];
        _phoneLab.textColor = UIColorFromRGB(0x363636);
        _phoneLab.font = mFont(15);
        _phoneLab.textAlignment = NSTextAlignmentLeft;
        _phoneLab.text = @"手机号";
        _phoneLab.size = CGSizeMake(100, 44);
        [_phoneLab sizeToFit];
    }
    return _phoneLab;
}

- (UITextField *)phoneNumTextField{
    if (!_phoneNumTextField) {
        _phoneNumTextField = [[UITextField alloc] init];
        _phoneNumTextField.borderStyle = UITextBorderStyleNone;
        _phoneNumTextField.placeholder = @"请输入您的手机号";
        _phoneNumTextField.font = mFont(15);
        _phoneNumTextField.textColor = UIColorFromRGB(0x363636);
        _phoneNumTextField.clearButtonMode = UITextFieldViewModeAlways;
        _phoneNumTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        _phoneNumTextField.clearsOnBeginEditing = YES;
        _phoneNumTextField.textAlignment = NSTextAlignmentLeft;
        _phoneNumTextField.adjustsFontSizeToFitWidth = YES;
        _phoneNumTextField.minimumFontSize = 20;
        _phoneNumTextField.keyboardType = UIKeyboardTypeNumberPad;
        _phoneNumTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _phoneNumTextField.returnKeyType = UIReturnKeyDone;
        _phoneNumTextField.keyboardAppearance = UIKeyboardAppearanceDefault;
        _phoneNumTextField.delegate = self;
        _phoneNumTextField.size = CGSizeMake(100, 30);
    }
    return _phoneNumTextField;
}


@end
